const helper = new Helper(this);
// 下拉框form 表单
const search = helper.get('search')?.widgetObj
const eventTitle = helper.get('eventTitle')?.widgetObj
const list = helper.get('list')?.widgetObj
const prefix = helper.getVariable('environmentType');
const videoPlatform = helper.getVariable('videoPlatform');
// 基础服务
const basicRequest = BasicServices.getInstance(prefix);
// 应急服务
const emergencyRequest = EmergencyServices.getInstance(prefix);
// 页面路由参数
let eventParams = helper.parseVariable("screen_params")

if (eventParams) {
    // 渲染标题
    eventTitle._option.links.text = eventParams.eventTitle
    eventTitle.render()
    // 设置事件数据
    list.setEvent(eventParams)

    // 请求事件级别列表
    basicRequest.getEventLevelList({}).then(json => {
        console.log('请求事件级别列表: => ', json)
        if (json.success) {
            // 找到当前事件对应目标级别
            const curLevel = json.result.rows?.find(v => v.id === eventParams.eventGradeCode)?.eventLevelCode
            const params = {
                eventType: eventParams.eventTypeCode,
                eventLevel: curLevel,
            }
            // 获取当前事件对应推荐预案
            emergencyRequest.getEmergencyPlan(params).then(res => {
                console.log('获取当前事件对应推荐预案: => ', res)
                if (res.success) {
                    const arr = res.result.map((v, i) => ({
                        ...v, value: v.id, name: i === 0 ? v.planName + '(推荐)' : v.planName,
                    }))

                    // 找到第一个推荐的
                    const first = arr[0]
                    if (first) {
                        // 为表单赋值
                        search.asyncSelectOptions('name', arr)
                        search.setFormData('form_1', {
                            name: first.id,
                            level: curLevel
                        })

                        // 设置预案列表的搜索条件
                        list.setParams(first.id, curLevel)
                    }

                    list.initQuery()
                }
            })
        }
    })


}
